class_name AIBehaviorField
extends AIBehavior

const PASS_PREBABILITY := 0.2
const SHOOT_DISTAN :=150
const SHOOT_PROBABILITY :=0.3

const SPREAD_ASSIST_FACTOR := 0.8

const TACKLE_DISTANCE := 15
const TACKLE_PROBABILITY := 0.3

func preform_ai_movement() -> void:
	var total_steering_force := Vector2.ZERO
	
	if player.has_ball():#带球队员会冲向球门
		total_steering_force += get_carrier_steering_force()
	#队友有球的时候，会跟着带球队友
	elif is_ball_carried_by_teammate():
		total_steering_force += get_assist_formation_steering()
	else:
		#非守门员球员会运动
		total_steering_force += get_onduty_steering_force()
		
		if total_steering_force.length() < 1:
			if is_ball_possessed_by_opponent():
				total_steering_force += get_spawn_steering_force()
			elif ball.carrier == null:
				total_steering_force += get_ball_proximity_steering_force()
				total_steering_force += get_density_around_ball_steering_force()
				
	
	total_steering_force = total_steering_force.limit_length(1.0)
	player.velocity = total_steering_force*player.speed
	

func perform_ai_decisions() -> void:
	#射门逻辑
	if randf()<TACKLE_PROBABILITY and is_ball_possessed_by_opponent() and player.position.distance_to(ball.position)< TACKLE_DISTANCE :
		player.switch_states(Player.State.TACKLING)
	
	
	if ball.carrier == player:
		var target := player.target_goal.get_center_target_position()
		#当离球面足够近了，行使射门逻辑
		if randf() < SHOOT_PROBABILITY and player.position.distance_to(target) < SHOOT_DISTAN :
			#强制面向对面球门，防止乌龙球
			player.face_towards_target_goal()
			var shot_direction := player.position.direction_to(player.target_goal.get_random_target_postition())
			
			var data = PlayerStateData.build().set_shot_power(player.power).set_shot_direction(shot_direction) as PlayerStateData
			player.switch_states(Player.State.SHOOTING,data)
		elif randf() < PASS_PREBABILITY and has_opponents_nearby() and  has_teammate_in_view():
			
			player.switch_states(Player.State.PASSING)



func get_onduty_steering_force()->Vector2:
	#一个玩家角色有多大动力向球方向跑
	return player.weight_on_duty_steering * player.position.direction_to(ball.position)


#拿球球员向对方球门进攻
func get_carrier_steering_force() ->Vector2:
	var target := player.target_goal.get_center_target_position()
	var direction := player.position.direction_to(target)
	var weight := get_bicircular_weight(player.position,target,100,0,150,1)
	
	return weight * direction




func get_assist_formation_steering() -> Vector2:
	var spawn_difference := ball.carrier.spawn_position - player.spawn_position
	var assist_destination := ball.carrier.position - spawn_difference * SPREAD_ASSIST_FACTOR
	
	var direction := player.position.direction_to(assist_destination)
	var weight := get_bicircular_weight(player.position,assist_destination,30,0.2,60,1)
	
	return weight*direction



#玩家靠近球時候，会更亲和球，去掌控球
func get_ball_proximity_steering_force() -> Vector2:
	var weight := get_bicircular_weight(player.position,ball.position,50,1,120,0)
	var direction := player.position.direction_to(ball.position)
	return weight * direction


#游戏进行久了，玩家会聚集在一起，我们尽量让其保持初始的队形
func get_spawn_steering_force() -> Vector2:
	var weight := get_bicircular_weight(player.position,player.spawn_position,30,0,120,1)
	var direction := player.position.direction_to(player.spawn_position)
	return weight * direction



func get_density_around_ball_steering_force() -> Vector2:
	var nb_teammates_near_ball := ball.get_proximity_teammates_count(player.country)
	if nb_teammates_near_ball == 0:
		return Vector2.ZERO
	var weight := 1.0-1.0/nb_teammates_near_ball
	var direction := ball.position.direction_to(player.position)
	return direction * weight


#视野中是否包含了队友以便传球
func has_teammate_in_view() -> bool:
	var player_in_view := teammate_detection_area.get_overlapping_bodies()
	
	return player_in_view.find_custom(func(p:Player):return p!=player and player.country == p.country)>-1
